#!/bin/bash
# +-------------------------------------------------------------------
# | 宝塔异地同步备份插件 - 安装脚本
# +-------------------------------------------------------------------
# | Version: 2.0.0
# | Author: yang
# +-------------------------------------------------------------------

PATH=/www/server/panel/pyenv/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# 配置插件安装目录
install_path=/www/server/panel/plugin/remoteback
pip_bin=/www/server/panel/pyenv/bin/pip3
python_bin=/www/server/panel/pyenv/bin/python3

# 颜色定义
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 打印信息函数
print_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

# 检查宝塔环境
check_bt_env() {
    print_info "检查宝塔环境..."
    
    if [ ! -f "$pip_bin" ]; then
        print_error "宝塔pip3不存在，请先安装宝塔面板"
        exit 1
    fi
    
    if [ ! -f "$python_bin" ]; then
        print_error "宝塔Python3不存在，请先安装宝塔面板"
        exit 1
    fi
    
    # 检查Python版本
    py_version=$($python_bin --version 2>&1 | awk '{print $2}')
    print_info "Python版本: $py_version"
    
    print_info "✅ 宝塔环境检查通过"
}

# 安装依赖
install_dependencies() {
    print_info "开始安装依赖包..."
    
    # 依赖列表
    declare -a dependencies=("croniter" "psutil")
    
    for dep in "${dependencies[@]}"; do
        print_info "安装 $dep..."
        $pip_bin install -q "$dep" || {
            print_warning "使用国内镜像重试安装 $dep..."
            $pip_bin install -q -i https://pypi.tuna.tsinghua.edu.cn/simple "$dep"
        }
        
        # 验证安装
        if $python_bin -c "import $dep" 2>/dev/null; then
            print_info "✅ $dep 安装成功"
        else
            print_error "❌ $dep 安装失败"
            return 1
        fi
    done
    
    print_info "✅ 所有依赖安装完成"
    return 0
}

# 创建必要目录
create_directories() {
    print_info "创建必要目录..."
    
    mkdir -p $install_path/config
    mkdir -p $install_path/backup
    mkdir -p /www/server/panel/logs
    
    # 设置权限
    chmod 755 $install_path
    chmod 755 $install_path/config
    chmod 755 $install_path/backup
    
    print_info "✅ 目录创建完成"
}

# 安装主函数
Install()
{
    echo "========================================"
    echo "  异地同步备份插件 v2.0.0"
    echo "  开始安装..."
    echo "========================================"
    echo ""
    
    check_bt_env || exit 1
    install_dependencies || exit 1
    create_directories || exit 1
    
    echo ""
    echo "========================================"
    print_info "🎉 安装完成！"
    echo "========================================"
    echo ""
    print_info "提示："
    echo "  1. 请在宝塔面板中启用插件"
    echo "  2. 配置目标服务器的API接口"
    echo "  3. 添加同步配置开始使用"
    echo ""
}

# 卸载主函数
Uninstall()
{
    echo "========================================"
    echo "  异地同步备份插件 v2.0.0"
    echo "  开始卸载..."
    echo "========================================"
    echo ""
    
    print_info "停止定时任务..."
    # 移除crontab中的定时任务
    if crontab -l 2>/dev/null | grep -q "schedule.py"; then
        crontab -l > /tmp/crontab_backup.txt 2>/dev/null
        sed -i '/schedule.py/d' /tmp/crontab_backup.txt
        crontab /tmp/crontab_backup.txt
        rm -f /tmp/crontab_backup.txt
        print_info "✅ 定时任务已移除"
    else
        print_info "未找到定时任务"
    fi
    
    print_info "清理日志文件..."
    # 清理日志
    rm -f /www/server/panel/logs/remoteback.log
    rm -f /www/server/panel/logs/remoteback_error.log
    print_info "✅ 日志文件已清理"
    
    print_info "清理插件文件..."
    # 删除插件目录
    if [ -d "$install_path" ]; then
        rm -rf $install_path
        print_info "✅ 插件文件已删除"
    fi
    
    echo ""
    echo "========================================"
    print_info "🎉 卸载完成！"
    echo "========================================"
    echo ""
    print_warning "注意："
    echo "  1. 配置文件已删除，无法恢复"
    echo "  2. 如需重新使用，请重新安装并配置"
    echo ""
}

# 操作判断
if [ "${1}" == 'install' ]; then
    Install
elif [ "${1}" == 'uninstall' ]; then
    Uninstall
else
    echo "========================================"
    echo "  异地同步备份插件 v2.0.0"
    echo "========================================"
    echo ""
    echo "使用方法："
    echo "  安装: bash install.sh install"
    echo "  卸载: bash install.sh uninstall"
    echo ""
    exit 1
fi
